package com.cloudloan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloudloan.common.R;
import com.cloudloan.entity.Book;
import com.cloudloan.mapper.BookMapper;
import com.cloudloan.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;


/**
 * <h3>cloudLoan</h3>
 * <p></p>
 *
 * @author gdw
 * @since 2024-08-26 11:20:15
 */
@Service
@Transactional
public class BookServiceImpl extends ServiceImpl<BookMapper, Book> implements BookService {

    @Autowired
    private BookMapper bookMapper;

    /**
     * 保存或者修改信息
     * @param book
     * @return
     */
    @Override
    public R inserOrUpdateBook(Book book) {
        Book book1 = bookMapper.selectById(book);
        if(book1==null){
            return R.error("未根据id查询出书籍信息");
        }else if(book.getTitle()==null||book.getTitle().equals("")){
            return R.error("未传递书籍title");
        }else if(book.getAuthor()==null||book.getAuthor().equals("")){
            return R.error("为传递作者名字");
        }else if(book.getType()==null){
            return R.error("为传递书籍的类型");
        }
        return bookMapper.updateById(book)>0?R.success("成功"):R.error("失败");
    }

    /**
     * 查询详细信息
     * @param id
     * @return
     */
    @Override
    public R getBookById(Long id) {
        Book book = bookMapper.selectById(id);
        if(book==null){
            return R.error("未根据id,查询出详细信息");
        }
        return R.success(book);
    }

    /**
     * 首页书籍展示
     * @param map
     * @return
     */
    @Override
    public R getBookTypeAll(Map<String, Object> map) {
        LambdaQueryWrapper<Book> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(map.get("type")!=null&&!"".equals(map.get("type")),
                Book::getType,map.get("type")).isNotNull(Book::getTitle);
        return R.success(bookMapper.selectList(queryWrapper));
    }
}
